package cn.shyouchi.wuguanjia.android.ui.home.security.thread.me;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.notice.NoticeApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import cn.shyouchi.wuguanjia.android.widget.dialog.TimePickerDialogFragment;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChangeNotificationSetActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_notice_name)
    TextView mTvNoticeName;
    @BindView(R.id.sw_notice_statue)
    Switch mSwNoticeStatue;
    @BindView(R.id.item_idcard)
    RelativeLayout mItemIdcard;
    @BindView(R.id.tv_notice_start_title)
    TextView mTvNoticeStartTitle;
    @BindView(R.id.tv_notice_start_value)
    TextView mTvNoticeStartValue;
    @BindView(R.id.item_person_info)
    RelativeLayout mItemPersonInfo;
    @BindView(R.id.tv_notice_end_title)
    TextView mTvNoticeEndTitle;
    @BindView(R.id.tv_notice_end_value)
    TextView mTvNoticeEndValue;
    @BindView(R.id.item_bank_num)
    RelativeLayout mItemBankNum;

    NoticeApi mNoticeApi;
    int type;


    MenuItem mMenuItem;


    public static void start(Context context, int type) {
        context.startActivity(new Intent(context, ChangeNotificationSetActivity.class).putExtra("type", type));
    }


    @Override
    public int initContentView() {
        return R.layout.activity_change_notification_set;
    }

    @Override
    public void initInjector() {
        mNoticeApi = new NoticeApi(mContext);
        type = getIntent().getIntExtra("type", type);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        fetchNoticeState();
        if (type == 0) {
            mTopBar.setTitle("考勤提醒");
            mTvNoticeName.setText("考勤提醒");
            mTvNoticeStartTitle.setText("签到时间");
            mTvNoticeEndTitle.setText("签退时间");
            mItemBankNum.setVisibility(View.VISIBLE);
        } else if (type == 1) {
            mTopBar.setTitle("报岗提醒");
            mTvNoticeName.setText("报岗提醒");
            mTvNoticeStartTitle.setText("报岗时间");
            mItemBankNum.setVisibility(View.GONE);
        } else {
            mTopBar.setTitle("巡检提醒");
            mTvNoticeName.setText("巡检提醒");
            mTvNoticeStartTitle.setText("巡检时间");
            mItemBankNum.setVisibility(View.GONE);
        }


        mSwNoticeStatue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleNoticeState();
            }
        });


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    private void fetchNoticeState() {
        mNoticeApi.getNoticeSignState(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<NoticeState>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<NoticeState> result) {
                        mSwNoticeStatue.setChecked(result.getResultMessage().getOpen() == 1);
                        if (type == 0) {
                            mTvNoticeStartValue.setText(result.getResultMessage().getStart());
                            mTvNoticeEndValue.setText(result.getResultMessage().getEnd());
                        } else {
                            mTvNoticeStartValue.setText(result.getResultMessage().getTime());
                        }
                    }
                });

    }


    public void setStartTime(View v) {
//        if (mSwNoticeStatue.isChecked()) {
        showTimePicker(mTvNoticeStartValue, "开始时间");
//        } else {
//            ToastUtil.showToast("请先打开提醒");
//        }
    }


    public void setEndTime(View v) {
//        if (mSwNoticeStatue.isChecked()) {
        showTimePicker(mTvNoticeEndValue, "结束时间");
//        } else {
//            ToastUtil.showToast("请先打开提醒");
//        }

    }


    public void showTimePicker(final TextView tv, String title) {
        TimePickerDialogFragment mTimePickerDialog;
        mTimePickerDialog = new TimePickerDialogFragment(getSupportFragmentManager(), title, "");
        mTimePickerDialog.setOnTimePickedListener(new TimePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String time) {
                tv.setText(time);
                tv.setTag(1);
            }
        });
        mTimePickerDialog.showDialog();
    }


    private void toggleNoticeState() {
        mNoticeApi.ToggleNoticeState(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseResult>(mContext) {
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSwNoticeStatue.setChecked(!mSwNoticeStatue.isChecked());
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        mSwNoticeStatue.setChecked(baseResult.getResultMessage().toString().equals("1"));
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenuItem = menu.add("保存");
        mMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == mMenuItem) {
            save();
        }
        return super.onOptionsItemSelected(item);
    }

    private void save() {
        Map<String, Object> params = new HashMap<>();
        if (type == 0) {
            params.put("start", mTvNoticeStartValue.getText().toString());
            params.put("end", mTvNoticeEndValue.getText().toString());
        } else {
            params.put("time", mTvNoticeStartValue.getText().toString());
        }
        mNoticeApi.saveNoticeState(type, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseResult>(mContext) {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        toggleNoticeState();
                    }
                });
    }
}
